package cn.gx.struts2.action;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.gx.domain.User;
import cn.gx.service.UserService;
import cn.gx.util.DataUtil;
import cn.gx.util.ValidateUtil;

@Controller("regAction")
@Scope("prototype")
public class RegAction extends BaseAction<User>{

	
	private static final long serialVersionUID = 6255816252207348803L;

	//注入
	@Resource(name="userService")
	private UserService userService;
	
	private String confirmPassword;
	
	

	/**
	 * 到达注册页面
	 * @return
	 */
	@SkipValidation
	public String toRegPage(){
		
		return "regPage";
	}
	
	public String doReg(){
		model.setPassword(DataUtil.md5(model.getPassword()));
		userService.saveEntity(model);
		return SUCCESS;
	}
	
	public void validateDoReg(){
		
		//非空
		if(!ValidateUtil.isValid(model.getEmail())){
			addFieldError("email", "邮箱是必填项");
		}
		if(!ValidateUtil.isValid(model.getPassword())){
			addFieldError("password", "密码是必填项");
		}
		if(!ValidateUtil.isValid(model.getNickname())){
			addFieldError("nickname", "昵称是必填项");
		}
		if(this.hasErrors()){
			return ;
		}
		//密码一致性
		if(!model.getPassword().equals(confirmPassword)){
			addFieldError("password", "密码输入不一致");
			return ;
		}
		//email 是否占用
		if(userService.isRegisted(model.getEmail())){
			addFieldError("email", "邮箱已占用!");
		}
	}

	
	
	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}
}
